﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyIM2013.Packets;

namespace TinyIM2013
{
    public class TokenDataCollection
    {
        private Dictionary<PCmd, TokenDataEntity> tokenDataDic;
        private static TokenDataCollection instance;
        public TokenDataCollection()
        {
            tokenDataDic = new Dictionary<PCmd, TokenDataEntity>();
        }
        public static TokenDataCollection Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new TokenDataCollection();
                }
                return instance;
            }
        }
        public TokenDataEntity GetTokenDataEntityByCommand(PCmd command)
        {
            foreach (var item in tokenDataDic)
            {
                if (item.Key == command)
                {
                    return item.Value;
                }
            }
            return null;
        }
        public void SetTokenDataWithCommandName(PCmd command, byte[] token)
        {
            TokenDataEntity entity = GetTokenDataEntityByCommand(command);
            if (entity == null)
            {
                entity = new TokenDataEntity();
                entity.PacketCommand = command;
                if (entity.Tokens != null)
                {
                    foreach (var item in entity.tokens)
                    {
                        if (item.SequenceEqual(token))
                        {
                            return;
                        }
                    }
                }
            }
            entity.Tokens.Add(token);
        }
    }
    public class TokenDataEntity
    {
        public List<byte[]> tokens { get; set; }
        public PCmd PacketCommand { get; set; }
        public TokenDataEntity()
        {
            tokens = new List<byte[]>();
        }
        public List<byte[]> Tokens
        {
            get
            {
                if (tokens == null)
                {
                    tokens = new List<byte[]>();
                }
                return tokens;
            }
        }
    }
}
